class CulturalHeritage::PeriodsController < ApplicationController
  #before_filter :authenticate_user!
  # GET /cultural_heritage/periods
  # GET /cultural_heritage/periods.xml
  def index
    @title_view = 'Periodos'
    @cultural_heritage_periods = CulturalHeritage::Period.all
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @cultural_heritage_periods }
        format.json  { render :json => @cultural_heritage_periods }
    end
  end

  # GET /cultural_heritage/periods/1
  # GET /cultural_heritage/periods/1.xml
  def show    
    @cultural_heritage_period = CulturalHeritage::Period.find(params[:id])
    @title_view = 'Periodos'
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @cultural_heritage_period }
       format.json  { render :json => @cultural_heritage_period }
    end    
  end

  # GET /cultural_heritage/periods/new
  # GET /cultural_heritage/periods/new.xml
  def new
    if (user_signed_in? && ([2].include?(current_user.role)))
      @cultural_heritage_period = CulturalHeritage::Period.new
      @title_view = 'Periodos'
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @cultural_heritage_period }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # GET /cultural_heritage/periods/1/edit
  def edit
    if (user_signed_in? && ([2].include?(current_user.role)))
      @cultural_heritage_period = CulturalHeritage::Period.find(params[:id])
      @title_view = 'Periodos'
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # POST /cultural_heritage/periods
  # POST /cultural_heritage/periods.xml
  def create    
    if (user_signed_in? && ([2].include?(current_user.role)))
      @title_view = 'Periodos'
      @cultural_heritage_period = CulturalHeritage::Period.new(params[:cultural_heritage_period])
#      eraInicio = @cultural_heritage_period.dcInicio
#      if eraInicio == '1'
#        @cultural_heritage_period.dcInicio = false
#      else
#        @cultural_heritage_period.dcInicio = true
#      end
      fechaInicio = params[:sstartdate]
      @cultural_heritage_period.startdate = DateTime.new((fechaInicio.to_i), 1 ,1)

#      eraFin = @cultural_heritage_period.dcFin
#      if eraFin == '1'
#        @cultural_heritage_period.dcFin = false
#      else
#        @cultural_heritage_period.dcFin = true
#      end
      fechaFin = params[:senddate]
      @cultural_heritage_period.enddate = DateTime.new((fechaFin.to_i), 1 ,1)      
      respond_to do |format|
        if @cultural_heritage_period.save
          format.html { redirect_to(@cultural_heritage_period,  :notice => 'Se ha creado correctamente el Periodo.') }
          format.xml  { render :xml => @cultural_heritage_period, :status => :created, :location => @cultural_heritage_period }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @cultural_heritage_period.errors, :status => :unprocessable_entity }
        end
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # PUT /cultural_heritage/periods/1
  # PUT /cultural_heritage/periods/1.xml
  def update
    if (user_signed_in? && ([2].include?(current_user.role)))
      @title_view = 'Periodos'
      @cultural_heritage_period = CulturalHeritage::Period.find(params[:id])
#      eraInicio = @cultural_heritage_period.dcInicio
#      if eraInicio == '1'
#        @cultural_heritage_period.dcInicio = false
#      else
#        @cultural_heritage_period.dcInicio = true
#      end
      fechainicio = params[:sstartdate]
      @cultural_heritage_period.startdate = DateTime.new((fechainicio.to_i), 1 ,1)
#      eraFin = @cultural_heritage_period.dcFin
#      if eraFin == '1'
#        @cultural_heritage_period.dcFin = false
#      else
#        @cultural_heritage_period.dcFin = true
#      end
      fechafin = params[:senddate]
      @cultural_heritage_period.enddate = DateTime.new((fechafin.to_i), 1 ,1)      
      respond_to do |format|
        if @cultural_heritage_period.update_attributes(params[:cultural_heritage_period])
          format.html { redirect_to(@cultural_heritage_period, :notice => 'Se ha actualizado correctamente el Periodo.') }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @cultural_heritage_period.errors, :status => :unprocessable_entity }
        end
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # DELETE /cultural_heritage/periods/1
  # DELETE /cultural_heritage/periods/1.xml
  def destroy
    if (user_signed_in? && ([2].include?(current_user.role)))
      @cultural_heritage_period = CulturalHeritage::Period.find(params[:id])
      mensaje = @cultural_heritage_period.erasable
      if mensaje.blank?
        @cultural_heritage_period.deleted = 1
        @cultural_heritage_period.save
      end
      respond_to do |format|
        format.html { redirect_to(cultural_heritage_periods_url, :notice => mensaje) }
        format.xml  { head :ok }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  def update_table
    @term = params[:search_name]
    if !@term.blank?
      @cultural_heritage_periods = CulturalHeritage::Period.find(:all,:conditions=>["cultural_heritage_periods.name LIKE ?",'%'+@term+'%'])
    else
      @cultural_heritage_periods= CulturalHeritage::Period.all
    end
    render :partial =>"table"
  end
end
